package com.peng.controller;

import com.peng.domain.ResponseResult;
import com.peng.domain.dto.ChangeStatusDto;
import com.peng.domain.entity.Role;
import com.peng.domain.vo.PageVo;
import com.peng.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    public ResponseResult list(Integer pageNum, Integer pageSize, Role role) {
        PageVo pageVo = roleService.pageRoleList(pageNum, pageSize, role);
        return ResponseResult.okResult(pageVo);
    }

    @PutMapping("/changeStatus")
    public ResponseResult changeStatus(@RequestBody ChangeStatusDto changeStatusDto) {
        Role role = new Role();
        role.setId(changeStatusDto.getRoleId());
        role.setStatus(changeStatusDto.getStatus());
        return ResponseResult.okResult(roleService.updateById(role));
    }

    /**
     * 新增角色
     * @param role
     * @return
     */
    @PostMapping
    public ResponseResult add(@RequestBody Role role) {
        roleService.insertRole(role);
        return ResponseResult.okResult();
    }

    /**
     * 根据角色编号获取详细信息
     */
    @GetMapping("/{id}")
    public ResponseResult getRoleId(@PathVariable("id") Long id) {
        Role role = roleService.getById(id);
        return ResponseResult.okResult(role);
    }

    /**
     * 更新角色
     * @param role
     * @return
     */
    @PutMapping
    public ResponseResult update(Role role){
        roleService.updateById(role);
        return ResponseResult.okResult();
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseResult remove(@PathVariable(name = "id") Long id) {
        roleService.removeById(id);
        return ResponseResult.okResult();
    }
    @GetMapping("/listAllRole")
    public ResponseResult listAllRole(){
        List<Role> roleList = roleService.listAllRole();
        return ResponseResult.okResult(roleList);
    }
}
